

/*
 * @Author: Kang
 * @Date: 2022-02-12 18:03:51
 * @Last Modified by: Kang
 * @LastEditTime: 2022-02-19 20:11:06
 */
module.exports = app => {
    class PlanController extends app.Controller {
        /**
         * 后台接口开始
         */
        * index() {
            const { ctx, app } = this;
            try {
                console.log(ctx.query)
                const token = ctx.request.header.authorization;
                const decode = yield app.jwt.verify(token, app.config.jwt.secret);
                const userInfo = yield this.service.login.getUserByName(decode.username);
                const res = yield this.service.plan.index(ctx.query,userInfo.user_id);
                const result = {
                    data: res.rows,
                    total: res.count,
                    success: true,
                    current: parseInt(ctx.query.current),
                    pageSize: parseInt(ctx.query.pageSize) 
                }
                ctx.body = result;
            } catch (error) {
                throw new Error(error);
                // ctx.body = {
				// 	status: 500,
				// 	desc: new Error(error),
				// 	data: null
				// }
            }
        }

        * create() {
            const { ctx, app } = this;
            const token = ctx.request.header.authorization;
            const decode = yield app.jwt.verify(token, app.config.jwt.secret);
            const userInfo = yield this.service.login.getUserByName(decode.username);
            ctx.request.body.user_id = userInfo.user_id
            ctx.request.body.create_time = new Date().getTime();
            ctx.request.body.update_time = new Date().getTime();
            console.log('ctx.request.body',ctx.request.body)
            const result = yield this.service.plan.create(ctx.request.body)
            ctx.body = result;
        }


        * update() {
            try {
                const { ctx } = this;
                ctx.request.body.update_time = new Date().getTime();
                const result = yield this.service.plan.update(ctx.request.body);
                ctx.body = result
            } catch (error) {
                throw new Error(error);
            }
        }

        * destroy() {
            const { ctx } = this;
            console.log('ctx.params.id',ctx.query)
            const result = yield this.service.plan.destroy(ctx.query.id);
            ctx.body = result;
        }
        /**
         * 后台接口结束
         */





        /**
         * 前台接口开始
         */
        * webPlan () {
            const { ctx, app } = this;
            try {
                console.log('ctx.query',ctx.query);
                const result = yield this.service.plan.webPlan(ctx.query);
                ctx.body = {
                    code: 200,
                    msg: '提交成功',
                    data: result
                }
            } catch (error) {
                ctx.body = {
					status: 500,
					desc: '访问失败',
					data: error
				}
            }
        }

        * getListAll() {
            const { ctx, app } = this
            try {
                const res = yield this.service.plan.getListAll(ctx.query);
                const result = {
                    data: res.rows,
                    total: res.count,
                    msg: '查询成功',
                    current: parseInt(ctx.query.current),
                    pageSize: parseInt(ctx.query.pageSize) 
                }
                ctx.body = result;
            } catch (error) {
                ctx.body = {
					status: 500,
					desc: '访问失败',
					data: error
				}
            }
        }

        * getPlanInfo () {
            const { ctx, app } = this
            try {
                const res = yield this.service.plan.getPlanInfo(ctx.query)
                const result = {
                    data: res,
                    msg: '查询成功'
                }
                ctx.body = result;
            } catch (error) {
                ctx.body = {
                    status: 500,
                    desc: '访问失败',
                    data: error
                }
            }
        }

        * getPlanData() {
            const { ctx, app } = this
            console.log(ctx.query)
            try {
                const res = yield this.service.plan.getPlanData(ctx.query);
                const result = {
                    data: res,
                    msg: '查询成功',
                }
                ctx.body = result;
            } catch (error) {
                ctx.body = {
					status: 500,
					desc: '访问失败',
					data: null
				}
            }
        }

        * getLatelyPlan(){
            const { ctx, app } = this
            try {
                console.log(ctx.query)
                const res = yield this.service.plan.getLatelyPlan(ctx.query);
                const result = {
                    data: res,
                    msg: '查询成功',
                }
                ctx.body = result;
            } catch (error) {
                ctx.body = {
					status: 500,
					desc: '访问失败',
					data: null
				}
            }
        }

    }
    /**
     * 前台接口结束
     */
    return PlanController;
}